package com.example.day04_fragment;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class MainActivity2 extends AppCompatActivity {
    OneFragment oneFragment;
    TwoFragment twoFragment;
    ThreeFragment threeFragment;
    private RadioGroup rg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initView();
        oneFragment = new OneFragment();
        twoFragment  = new TwoFragment();
        threeFragment = new ThreeFragment();
        //默认显示oneFragment---使用replace实现页面切换
        /*replaceFragment(new OneFragment());
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        replaceFragment(new OneFragment());
                        break;
                    case R.id.rb2:
                        replaceFragment(new TwoFragment());
                        break;
                    case R.id.rb3:
                        replaceFragment(new ThreeFragment());
                        break;
                }
            }
        });*/
        //默认显示第一个
        getSupportFragmentManager().beginTransaction()
                //参数一 布局id 参数二 fragment 参数三 标志名
                .add(R.id.frame,oneFragment,"one")
                .add(R.id.frame,twoFragment,"two")
                .add(R.id.frame,threeFragment,"three")
                .hide(twoFragment)
                .hide(threeFragment)
                .hide(oneFragment)
                .show(oneFragment)
                .commit();
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        showFragment(oneFragment);
                        break;
                    case R.id.rb2:
                       showFragment(twoFragment);
                        break;
                    case R.id.rb3:
                        showFragment(threeFragment);
                        break;
                }
            }
        });


    }

    private void showFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .hide(oneFragment)
                .hide(threeFragment)
                .hide(twoFragment)
                .show(fragment)
                .commit();
    }

    private void replaceFragment(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.frame,fragment)
                .addToBackStack(null)//将fragment添加到回退栈
                .commit();
    }


    private void initView() {
        rg = (RadioGroup) findViewById(R.id.rg);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            //获取回退栈中的数量
            int count = getSupportFragmentManager().getBackStackEntryCount();
            if(count>1){
                //回退到首页
                while (getSupportFragmentManager().getBackStackEntryCount()>1){
                    getSupportFragmentManager().popBackStackImmediate();
                    rg.check(R.id.rb1);
                }
            }else{
                finish();//关闭当前activity
            }
        }
        return true;
    }
    //系统菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.shanchu:
                Toast.makeText(this, "点击了删除", Toast.LENGTH_SHORT).show();
                break;
            case R.id.xiangqing:
                Toast.makeText(this, "点击了详情", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tongzhi:
                Toast.makeText(this, "点击了通知", Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}